import json

summary_dir = '/nfs/home/xuezhen/pro/xshan/spec_scripts/summary.json'
max_point_per_workload = 20
target_coverage = 0.3
out_json = f"./simpoint_coverage{target_coverage}_test.json"
total_cases = 0

with open(summary_dir, 'r') as f:
  all_cases = json.load(f)
  coverage_buffer = {}
  total_points = 0

  for case in all_cases.keys():
    total_cases += 1
    points = all_cases[case]
    sorted_points = sorted(points.items(), key = lambda kv:(kv[1], kv[0]), reverse=True)

    weight_sum = 0
    points_num = 0
    coverage_buffer[case] = {}
    for point in sorted_points:
      coverage_buffer[case][point[0]] = point[1]
      weight_sum   += float(point[1])
      points_num   += 1
      total_points += 1
      if(weight_sum >= target_coverage or points_num >= max_point_per_workload):
        break

  print("total points: ", total_points)
  with open(out_json, 'w') as out_f:
    json.dump(coverage_buffer, out_f, indent=4)

print("total cases: ", total_cases)
